#include <stdio.h>
#include "netcard.h"
extern void Output(const char * ftm, ... );

cnetcard::cnetcard(void)
{
}


cnetcard::~cnetcard(void)
{
}

int cnetcard::getnetcards(std::vector<std::string>& v ){
#ifdef WIN32
	HANDLE   hMprConfig;   
	DWORD   dwRet=0;   
	PIP_INTERFACE_INFO   plfTable=NULL;   
	IP_ADAPTER_INDEX_MAP   AdaptMap;   
	DWORD   dwBufferSize=0;   
	WCHAR   szFriendName[256];   
	DWORD   tchSize=sizeof(WCHAR)*256;   
	ZeroMemory(&szFriendName,tchSize);   
	dwRet=MprConfigServerConnect   (NULL,&hMprConfig);   
	dwRet=GetInterfaceInfo(NULL,&dwBufferSize);   
	if(dwRet==ERROR_INSUFFICIENT_BUFFER)   {   
		plfTable=(PIP_INTERFACE_INFO)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,dwBufferSize);   
		GetInterfaceInfo(plfTable,&dwBufferSize);   
	}      
	for(UINT   i=0;i<plfTable->NumAdapters;i++){   
		AdaptMap=plfTable->Adapter[i];   
		dwRet=MprConfigGetFriendlyName(hMprConfig,AdaptMap.Name,(PWCHAR)szFriendName,tchSize);   
		char szName[1024];
		memset( szName, 0, 256 );
		int nLen = WideCharToMultiByte( CP_ACP,NULL, szFriendName,-1,NULL,0,NULL,FALSE );
		WideCharToMultiByte (CP_ACP,NULL,szFriendName,-1,szName,nLen,NULL,FALSE);
		Output("insert:cardname=%s",szName );
		v.push_back(szName);
	}   
	HeapFree(GetProcessHeap(),HEAP_ZERO_MEMORY,plfTable);   
#else
	int i=0;
	int sockfd;
	struct ifconf ifconf;
	char buf[512];
	struct ifreq *ifreq;
	ifconf.ifc_len = 512;
	ifconf.ifc_buf = buf;
	if((sockfd = socket(AF_INET, SOCK_DGRAM, 0))<0){
		printf("socket crate error");
		return false;
	}  
	ioctl(sockfd, SIOCGIFCONF, &ifconf);
	ifreq = (struct ifreq*)buf;  
	for(i=(ifconf.ifc_len/sizeof(struct ifreq)); i>0; i--){
		v.push_back(ifreq->ifr_name);
		//printf("local addr = [%s]\n", inet_ntoa(((struct sockaddr_in*)&(ifreq->ifr_addr))->sin_addr));
		ifreq++;
	}
	close(sockfd);
#endif
	return true;
}
